<?php
/**
 * Auction
 * 
 * @author Eric
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class Application_Model_Auction extends Zend_Db_Table_Abstract {
	
	/**
	 * The default table name 
	 */
	protected $_name = 'auction';
	
	// 子表
	protected $_dependentTables = array('Application_Model_Winner');
	
	// 父表
	protected $_referenceMap = array(
		'brand' => array(
			'columns' => array('brand_id'),
           	'refTableClass' => 'Application_Model_Brand',
           	'refColumns' => array('id'),
           	'onUpdate' => self::CASCADE, //更新主表记录时是否更新关联记录
    	),
    	'user' => array(
			'columns' => array('current_user_id'),
           	'refTableClass' => 'Application_Model_User',
           	'refColumns' => array('id'),
           	'onUpdate' => self::CASCADE, //更新主表记录时是否更新关联记录
    	),
    	'category' => array(
			'columns' => array('category_id'),
           	'refTableClass' => 'Application_Model_Category',
           	'refColumns' => array('id'),
           	'onUpdate' => self::CASCADE, //更新主表记录时是否更新关联记录
    	)
	);
	
	public function getAuctionByStatus($status) {
		
		$db = $this->getAdapter();
		$sql = $db->quoteInto("status = ?", $status);
		$order = 'priority asc';
		
		$rows = $this->fetchAll($sql, $order);
		return $rows;
	}
	
	public function getAuctionById($id) {
		
		$db = $this->getAdapter();
		$sql = $db->quoteInto("id = ?", $id);
		
		$row = $this->fetchRow($sql);
		return $row;
	}
	
	public function getAuctionOrId($id, $status) {
		
		$db = $this->getAdapter();
		$sql = $db->quoteInto("id = ?", $id) . " or " . $db->quoteInto("status = ?", $status);
		$order = 'priority asc';
		
		$rows = $this->fetchAll($sql, $order);
		return $rows;
	}
	
	public function deleteAuctionById($id) {
		$this->delete('id=' . $id);
	}
}
